//Anti-Health/Armor/Vehicle Health Cheats Module
//Ripped from LACY anticheat (by LucifeR)

forward AntiHealthCheats_OnGameModeInit();
forward AntiHealthCheats_Timer();
forward CheckPlayerForHealthCheats(playerid, num);

new TimerAntiHealth;

new Anti_Health_Enabled=0;
new Anti_Armor_Enabled=0;
new Anti_VehicleHP_Enabled=0;
new Action_On_HealthCheats=2;
new HealthCheats_Ban_Hours=720;

#define HEALTH_MAX_WARNINGS 2

new Float:Health_Player[MAX_PLAYERS];
new Float:Health_Armor[MAX_PLAYERS];
new Float:Health_Vehicle[MAX_PLAYERS];
new health_warnings[MAX_PLAYERS][2];
new Float:health_facing[MAX_PLAYERS][3];

public AntiHealthCheats_OnGameModeInit(){
	AntiHealthCheatsConfig();
	if (Anti_Health_Enabled == 1 || Anti_Armor_Enabled == 1 || Anti_VehicleHP_Enabled == 1){
		TimerAntiHealth = SetTimer("AntiHealthCheats_Timer",3000,1);
		WriteLog("Anti health cheats protection system Loaded");
	}
	return 0;
}

AntiHealthCheatsConfig()
{
	Debug("protections/health.inc > AntiHealthCheatsConfig - Start");
	new temp[MAX_STRING];
	if (!db_Exists(SecurityDB)) db_Create(SecurityDB);
	
	set(temp,db_Get(SecurityDB,"Anti_Health_Enabled"));
	if (strlen(temp) > 0) Anti_Health_Enabled = strval(temp);
	else { valstr(temp,Anti_Health_Enabled); db_Set(SecurityDB,"Anti_Health_Enabled",temp);  }
	set(temp,nullstr);

	set(temp,db_Get(SecurityDB,"Anti_Armor_Enabled"));
	if (strlen(temp) > 0) Anti_Armor_Enabled = strval(temp);
	else { valstr(temp,Anti_Armor_Enabled); db_Set(SecurityDB,"Anti_Armor_Enabled",temp);  }
	set(temp,nullstr);

	set(temp,db_Get(SecurityDB,"Anti_VehicleHP_Enabled"));
	if (strlen(temp) > 0) Anti_VehicleHP_Enabled = strval(temp);
	else { valstr(temp,Anti_VehicleHP_Enabled); db_Set(SecurityDB,"Anti_VehicleHP_Enabled",temp);  }
	set(temp,nullstr);

	set(temp,db_Get(SecurityDB,"Action_On_HealthCheats"));
	if (strlen(temp) > 0) Action_On_HealthCheats = strval(temp);
	else { valstr(temp,Action_On_HealthCheats); db_Set(SecurityDB,"Action_On_HealthCheats",temp);  }
	set(temp,nullstr);

	set(temp,db_Get(SecurityDB,"HealthCheats_Ban_Hours"));
	if (strlen(temp) > 0) HealthCheats_Ban_Hours = strval(temp);
	else { valstr(temp,HealthCheats_Ban_Hours); db_Set(SecurityDB,"HealthCheats_Ban_Hours",temp);  }
	set(temp,nullstr);

	Debug("protections/health.inc > AntiHealthCheatsConfig - Stop");
	return;
}

public AntiHealthCheats_Timer(){
	Debug("protections/health.inc > AntiHealthCheats_Timer - Start");
	for(new playerid=0; playerid <= MAX_PLAYERS_EX; playerid++)
	{
	    	if(IsPlayerConnected(playerid) && (GetPlayerState(playerid) >= PLAYER_STATE_NONE || GetPlayerState(playerid) <= PLAYER_STATE_PASSENGER))
			if (!IsPlayerAdmin(playerid) && PlayerPriv[playerid][IsModerator] == 0){
				CheckPlayerForHealthCheats(playerid, 1);
			}
	}
	Debug("protections/health.inc > AntiHealthCheats_Timer - Stop");
}

public CheckPlayerForHealthCheats(playerid, num)
{
	if(num == 1 && IsPlayerConnected(playerid))
	{
		if (Anti_Health_Enabled == 1){
			GetPlayerHealth(playerid, Health_Player[playerid]);
			SetPlayerHealth(playerid, Health_Player[playerid]-1);
		}
		
		if (Anti_Armor_Enabled == 1){
			GetPlayerArmour(playerid, Health_Armor[playerid]);
			if(Health_Armor[playerid] > 2) SetPlayerArmour(playerid, Health_Armor[playerid]-1);
		}
		
		if (Anti_VehicleHP_Enabled == 1){	
        		if(IsPlayerInAnyVehicle(playerid)){
				new vid = GetPlayerVehicleID(playerid);
            			GetVehicleHealth(vid, Health_Vehicle[playerid]);
				SetVehicleHealth(vid, Health_Vehicle[playerid]-1);
			}
		}		
	
		GetPlayerFacingAngle(playerid, health_facing[playerid][0]);
        
		SetTimerEx("CheckPlayerForHealthCheats", 1000, 0, "d d", playerid, 2);
	}
	if(num == 2 && IsPlayerConnected(playerid))
	{
	    	health_warnings[playerid][0]++;
	    	new Float:tmphealth;
		new tmpcheat = 0;
		new tmpstr[256];
	
		GetPlayerFacingAngle(playerid, health_facing[playerid][1]);

		if (Anti_Health_Enabled == 1){
			GetPlayerHealth(playerid,tmphealth);
			if (tmphealth > Health_Player[playerid]-1 && tmphealth > 1){
		    		format(tmpstr, 256, gettext(1346), gettext(1309));
		    		tmpcheat = 1;
				SetPlayerHealth(playerid, Health_Player[playerid]);
			}

		}
		
		if(Anti_Armor_Enabled == 1 && Health_Armor[playerid] > 1)
		{
		    	GetPlayerArmour(playerid, tmphealth);
			if(tmphealth > Health_Armor[playerid]-1 && tmphealth > 1)
			{
			    format(tmpstr, 256, gettext(1347), gettext(1309));
			    tmpcheat = 2;
			}
			SetPlayerArmour(playerid, Health_Armor[playerid]);
		}
		
		if(Anti_VehicleHP_Enabled == 1)
		{
			if (IsPlayerInAnyVehicle(playerid)){
				new vid = GetPlayerVehicleID(playerid);
			   	GetVehicleHealth(vid, tmphealth);
				if(tmphealth > Health_Vehicle[playerid]-1 && tmphealth > 1)
				{
				    format(tmpstr, 256, gettext(1348), gettext(1309));
				    tmpcheat = 3;
				}
				SetVehicleHealth(vid, Health_Armor[playerid]);
			}
		}

		if(tmpcheat > 0)
			health_warnings[playerid][1]++;
		
		if(health_warnings[playerid][0] == HEALTH_MAX_WARNINGS)
		{
		    	if(health_warnings[playerid][1] == HEALTH_MAX_WARNINGS)
            		{
                   		if(health_facing[playerid][0] != health_facing[playerid][1] || health_facing[playerid][0] != health_facing[playerid][2]	|| health_facing[playerid][1] != health_facing[playerid][2])
				{
					if (Action_On_HealthCheats == 0){return;}
					if (Action_On_HealthCheats == 1){Report(playerid,tmpstr);return;}
					if (Action_On_HealthCheats == 2){KickPlayer(playerid,-1,tmpstr);return;}
					if (Action_On_HealthCheats == 3){BanPlayer(playerid,HealthCheats_Ban_Hours,tmpstr);return;}
				}
			}
		    
			health_warnings[playerid][0] = 0;
			health_warnings[playerid][1] = 0;
		}
		
		GetPlayerFacingAngle(playerid, health_facing[playerid][2]);

	}
}